package com.xiaoshuidi.cloud.module.finance.controller.app.order;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.finance.controller.app.order.vo.AppPayOrderSubmitReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.order.vo.AppPayOrderSubmitRespVO;
import com.xiaoshuidi.cloud.module.finance.framework.web.config.PayWhitelist;
import com.xiaoshuidi.cloud.module.finance.service.order.PayOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.common.util.servlet.ServletUtils.getClientIP;

@Tag(name = "租客 APP - 支付")
@RestController
@RequestMapping("/pay/order")
@Validated
@Slf4j
public class AppPayOrderController {

    @Resource
    private PayOrderService orderService;

    @PostMapping
    @Operation(summary = "账单/预定支付")
    public CommonResult<AppPayOrderSubmitRespVO> submitPayOrder(@RequestBody AppPayOrderSubmitReqVO reqVO) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        if (user == null) {
            throw new ServiceException(GlobalErrorCodeConstants.UNAUTHORIZED);
        }
        AppPayOrderSubmitRespVO respVO=  new AppPayOrderSubmitRespVO();
       if (reqVO.getChannelCode().equalsIgnoreCase("tongshangyun_wx_miniapp")||reqVO.getChannelCode().equalsIgnoreCase("tongshangyun_alipay_miniapp")){
           respVO = orderService.submitTongshangyunPayOrder(reqVO, user, getClientIP());
       }else {
           respVO = orderService.submitPayOrder(reqVO, user, getClientIP());
       }

        return success(respVO);
    }

}
